#!/bin/bash

APP_INSTALL_NAME="spark-tts"
APP_TARGET_DIR_NAME="spark-tts"
CONDA_ENV_NAME=$APP_INSTALL_NAME
CONDA_PREFIX="./venv"
echo "lmdprogress:10"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/alias.sh"
echo "lmdprogress:20"

env_list=$(conda env list | awk '{print $1}' | grep -v '^#' | grep -v '^base' | grep -v '^ ')

if echo "$env_list" | grep -q "^${CONDA_ENV_NAME}$"; then
    echo "env found '$CONDA_ENV_NAME'"
    conda env remove -n "$CONDA_ENV_NAME" -y
    echo "env '$CONDA_ENV_NAME' has been removed"
else
    echo "env '$CONDA_ENV_NAME' dose not exist"
fi

echo "lmdprogress:40"

export CURRENT_APP_PATH="${LMD_APPS_DIR}/${APP_TARGET_DIR_NAME}"
echo $CURRENT_APP_PATH


if [ -d "$CURRENT_APP_PATH" ]; then
    echo "App Dir: "$CURRENT_APP_PATH
else
    echo "Error, App Dir dose not exist $CURRENT_APP_PATH"
    echo "lmdprogress:100"
    exit 1
fi

echo "lmdprogress:45"

cd "$CURRENT_APP_PATH"


if [ -d "$CONDA_PREFIX" ]; then
    echo "found: ./venv"
    echo "remove ./venv"
    conda env remove -p "$CONDA_PREFIX" -y
    echo "removed"
else
    echo "./venv dose not exist"
fi
echo "lmdprogress:50"


if [ -d "$CURRENT_APP_PATH" ]; then
    # remove models
    MODELS_DIR="./checkpoints"
    echo 'remove models'
    echo "App Dir: "$CURRENT_APP_PATH
    rm -rf $MODELS_DIR
    echo "lmdprogress:80"

    # remove app files
    cd "$LMD_APPS_DIR"
    echo 'remove app files'
    rm -rf "$CURRENT_APP_PATH"
    echo "lmdprogress:100"
fi

